package com.project.zsxd.server.user.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.zsxd.server.common.bean.Result;
import com.project.zsxd.server.common.enums.UploadTypeEnum;
import com.project.zsxd.server.common.helper.FileHelper;
import com.project.zsxd.server.common.util.CommonUtil;
import com.project.zsxd.server.user.entity.User;
import com.project.zsxd.server.user.service.IUserService;
import com.project.zsxd.server.user.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;

/**
 * <p>
 *  用户前端控制器
 * </p>
 *
 * @author kaler
 * @since 2020-05-12
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    @RequestMapping("/listByPage")
    public Result listByPage(User user, Page<User> page) {
        IPage<User> resultPage = userService.page(page, new QueryWrapper<User>(user));
        return Result.success(resultPage);
    }

    @RequestMapping("/getByUserName")
    public Result getByUserName(String userName) {
        //查询数据
        User user = userService.getById(userName);
        //封装VO
        UserVO userVO = new UserVO();
        CommonUtil.copyBean(user, userVO);
        return Result.success(userVO);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody User user) {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        if (!StrUtil.isEmpty(user.getNickName())) {
            currentUser.setNickName(user.getNickName());
        }
        if (!StrUtil.isEmpty(user.getHeadIcon())) {
            currentUser.setHeadIcon(user.getHeadIcon());
        }
        currentUser.setUpdatedBy(currentUser.getUserName());
        currentUser.setUpdatedTime(new Date());
        //查询数据
        userService.updateById(currentUser);
        return Result.success(null);
    }

    /**
     * 上传头像
     * @param headIconFile 头像文件
     * @return 头像访问路径
     * @throws IOException 上传异常
     */
    @RequestMapping("/uploadHeadIcon")
    public Result uploadHeadIcon(@RequestParam("headIcon") MultipartFile headIconFile) throws IOException {
        String suffix = Objects.requireNonNull(headIconFile.getOriginalFilename()).substring(headIconFile.getOriginalFilename().lastIndexOf("."));
        File headFile = new File(FileHelper.getUploadPath(UploadTypeEnum.HeadIcon, suffix));
        if (!headFile.getParentFile().exists() && !headFile.getParentFile().mkdirs()) {
            log.error("headFile save dir[{}] create failed.", headFile.getParentFile().getAbsolutePath());
        }
        headIconFile.transferTo(headFile);
        String downloadUrl = FileHelper.getRelativeDownloadUrl(UploadTypeEnum.HeadIcon, headFile.getName());
        return Result.success(downloadUrl);
    }


}
